package org.jeecg.modules.demo.hd.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.hd.entity.AppUserCashCashoutDetail;
import org.jeecg.modules.demo.hd.service.IAppUserCashCashoutDetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.hd.service.IAppUserCashMainService;
import org.jeecg.modules.demo.hd.vo.myProject.CashOutVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 用户提现申请
 * @Author: jeecg-boot
 * @Date: 2025-05-17
 * @Version: V1.0
 */
@Api(tags = "用户提现申请")
@RestController
@RequestMapping("/hd/UserCashCashoutDetail")
@Slf4j
public class AppUserCashCashoutDetailController extends JeecgController<AppUserCashCashoutDetail, IAppUserCashCashoutDetailService> {
    @Autowired
    private IAppUserCashCashoutDetailService appUserCashCashoutDetailService;

    @Autowired
    private IAppUserCashMainService appUserCashMainService;

    @Autowired
    private ISysUserService userService;

    /**
     * 分页列表查询
     *
     * @param appUserCashCashoutDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户提现申请-分页列表查询")
    @ApiOperation(value = "用户提现申请-分页列表查询", notes = "用户提现申请-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppUserCashCashoutDetail>> queryPageList(AppUserCashCashoutDetail appUserCashCashoutDetail,
                                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                 HttpServletRequest req) {
        QueryWrapper<AppUserCashCashoutDetail> queryWrapper = QueryGenerator.initQueryWrapper(appUserCashCashoutDetail, req.getParameterMap());
        Page<AppUserCashCashoutDetail> page = new Page<AppUserCashCashoutDetail>(pageNo, pageSize);
        IPage<AppUserCashCashoutDetail> pageList = appUserCashCashoutDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param appUserCashCashoutDetail
     * @return
     */
    @AutoLog(value = "用户提现申请-添加")
    @ApiOperation(value = "用户提现申请-添加", notes = "用户提现申请-添加")
    @RequiresPermissions("hd:app_user_cash_cashout_detail:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AppUserCashCashoutDetail appUserCashCashoutDetail) {
        appUserCashCashoutDetailService.save(appUserCashCashoutDetail);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "用户提现申请-用户申请")
    @ApiOperation(value = "用户提现申请-用户申请", notes = "用户提现申请-用户申请")
    @PostMapping(value = "/apply")
    public Result<String> apply(@RequestBody CashOutVo vo, HttpServletRequest req) {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        AppUserCashCashoutDetail model = new AppUserCashCashoutDetail();
        // 判断余额失败够
        int aFloat = (int) (appUserCashMainService.queryCurrentCash()).intValue();
        if (aFloat < vo.getCashOutNum()) {
            return Result.error("提现失败，余额不足！");
        }
        SysUser user = userService.getById(currentUser.getId());
        if (user.getStatus().equals(2)) {
            return Result.error("提现失败，用户状态已冻结！");
        }
        // 判断今天有没有提现，如果有提示已经提现
        Integer cashOutDetailCount = appUserCashCashoutDetailService.getCashOutDetailCount(currentUser.getId());
        if (cashOutDetailCount > 0) {
            return Result.error("提现失败，每用户每日仅限提现一次！");
        }
        model.setUserId(currentUser.getId());
        model.setUserName(currentUser.getUsername());
        model.setCashNum((int) vo.getCashOutNum());
        model.setStatus("0");
        appUserCashCashoutDetailService.save(model);
        return Result.OK("申请提现成功！");
    }

    /**
     * 编辑
     *
     * @param appUserCashCashoutDetail
     * @return
     */
    @AutoLog(value = "用户提现-编辑")
    @ApiOperation(value = "用户提现-编辑", notes = "用户提现-编辑")
    @RequiresPermissions("hd:app_user_cash_cashout_detail:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AppUserCashCashoutDetail appUserCashCashoutDetail) {
        appUserCashCashoutDetailService.updateById(appUserCashCashoutDetail);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "用户提现-更新状态")
    @ApiOperation(value = "用户提现-更新状态", notes = "用户提现-编辑")
    @RequiresPermissions("hd:app_user_cash_cashout_detail:edit")
    @RequestMapping(value = "/updateStatus", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> updateStatus(@RequestBody AppUserCashCashoutDetail appUserCashCashoutDetail) {
        AppUserCashCashoutDetail model = appUserCashCashoutDetailService.getById(appUserCashCashoutDetail.getId());
        if (model == null) {
            return Result.error("未找到对应数据");
        }
        model.setStatus(appUserCashCashoutDetail.getStatus());
        appUserCashCashoutDetailService.updateById(model);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户提现-通过id删除")
    @ApiOperation(value = "用户提现-通过id删除", notes = "用户提现-通过id删除")
    @RequiresPermissions("hd:app_user_cash_cashout_detail:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appUserCashCashoutDetailService.removeById(id);
        return Result.OK("删除成功!");
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "app_user_cash_cashout_detail-通过id查询")
    @ApiOperation(value = "用户提现-通过id查询", notes = "用户提现-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AppUserCashCashoutDetail> queryById(@RequestParam(name = "id", required = true) String id) {
        AppUserCashCashoutDetail appUserCashCashoutDetail = appUserCashCashoutDetailService.getById(id);
        if (appUserCashCashoutDetail == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appUserCashCashoutDetail);
    }
}
